package net.aiion.weave.impl.bytecode.events;

import java.lang.annotation.Annotation;

import com.sun.xml.internal.ws.org.objectweb.asm.Type;

import net.aiion.weave.spi.bytecode.BytecodeClassVisitor;
import net.aiion.weave.spi.bytecode.BytecodeFieldVisitor;
import net.aiion.weave.spi.bytecode.BytecodeMethodVisitor;
import net.aiion.weave.spi.bytecode.BytecodeVisitor;
import net.aiion.weave.spi.bytecode.events.AnnotationStartBytecodeEvent;

public class AnnotationStartBytecodeEventImpl extends BytecodeEventImpl implements AnnotationStartBytecodeEvent
{
    private String descriptor;
    private boolean visible;

    public AnnotationStartBytecodeEventImpl(final String aDescriptor, final boolean aVisible) {
        descriptor = aDescriptor;
        visible = aVisible;
    }

    @Override
    public int getType() {
        return ANNOTATION_START;
    }

    @Override
    public void setDescriptor(final String aDescriptor) {
        descriptor = aDescriptor;
    }

    @Override
    public String getDescriptor() {
        return descriptor;
    }

    @Override
    public boolean equalsDescriptor(final String aDescriptor) {
        return aDescriptor.equals(descriptor);
    }

    @Override
    public boolean equalsType(final Class<? extends Annotation> aType) {
        return equalsDescriptor(Type.getDescriptor(aType));
    }

    @Override
    public void setVisible(final boolean aVisible) {
        visible = aVisible;
    }

    @Override
    public boolean getVisible() {
        return visible;
    }

    @Override
    public BytecodeVisitor acceptBytecodeVisitor(final BytecodeVisitor aBytecodeVisitor) {
        final BytecodeClassVisitor bytecodeClassVisitor = aBytecodeVisitor.asBytecodeClassVisitor();
        if (bytecodeClassVisitor != null) {
            return bytecodeClassVisitor.visitAnnotationStart(this);
        }

        final BytecodeFieldVisitor bytecodeFieldVisitor = aBytecodeVisitor.asBytecodeFieldVisitor();
        if (bytecodeFieldVisitor != null) {
            return bytecodeFieldVisitor.visitAnnotationStart(this);
        }

        final BytecodeMethodVisitor bytecodeMethodVisitor = aBytecodeVisitor.asBytecodeMethodVisitor();
        if (bytecodeMethodVisitor != null) {
            return bytecodeMethodVisitor.visitAnnotationStart(this);
        }

        return aBytecodeVisitor;
    }
}
